package com.nazca.tvremote.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.nazca.tvremote.R;
import com.nazca.tvremote.event.RemoteKeyEvent;
import com.nazca.tvremote.service.Controller;

public class TVRemoteControl extends LinearLayout implements OnClickListener {

	private Controller mController;
	
	public void setController(Controller controller){
		mController = controller;
	}
	
	public View mContainer;
	private FrameLayout mControlView;
	
	private View mChVolView;
	private Button mChVolToggleBtn;
	private View mChVolPad;
	
	private View mNavigationView;
	private Button mNavigationToggleBtn;
	private View mNavigationPad;
	
	public TVRemoteControl(Context context) {
		super(context);
	}
	
	public TVRemoteControl(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		mContainer = findViewById(R.id.tv_remote_control);
		mChVolView = mContainer.findViewById(R.id.ch_vol);
		mControlView = (FrameLayout) mContainer.findViewById(R.id.control);
		mNavigationView = mContainer.findViewById(R.id.navigation);
		
		mChVolToggleBtn = (Button) mChVolView.findViewById(R.id.toggle);
		mNavigationToggleBtn = (Button) mNavigationView.findViewById(R.id.toggle);
		
		mChVolPad = mChVolView.findViewById(R.id.pad);
		mNavigationPad = mNavigationView.findViewById(R.id.pad);
		
		initControlPad();
		
		initChVol();
		initNavigation();
	}
	
	private void initChVol(){
		mChVolToggleBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				setChVolShow(!isChVolShown());
				setNavigationShow(false);
			}
		});
	}
	
	private void initNavigation(){
		mNavigationToggleBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				setNavigationShow(!isNavigationShown());
				setChVolShow(false);
			}
		});
	}
	
	private boolean isChVolShown(){
		return mChVolPad.getVisibility() == View.VISIBLE;
	}
	
	private boolean isNavigationShown(){
		return mNavigationPad.getVisibility() == View.VISIBLE;
	}
	
	private void setChVolShow(boolean show){
		mChVolPad.setVisibility(show ? View.VISIBLE : View.GONE);
		mChVolToggleBtn.setBackgroundResource(show ? R.drawable.btn_ch_vol_hide : R.drawable.btn_ch_vol_show);
		mChVolToggleBtn.setText(show ? null : getContext().getString(R.string.tv_remote_ch_vol));
		initControlPad();
	}
	
	private void setNavigationShow(boolean show){
		mNavigationPad.setVisibility(show ? View.VISIBLE : View.GONE);
		View view;
		view = mNavigationView.findViewById(R.id.a);
		view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		view.setOnClickListener(this);
		view = mNavigationView.findViewById(R.id.b);
		view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		view.setOnClickListener(this);
		view = mNavigationView.findViewById(R.id.c);
		view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		view.setOnClickListener(this);
		view = mNavigationView.findViewById(R.id.d);
		view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		view.setOnClickListener(this);
		mNavigationToggleBtn.setText(show ? null : getContext().getString(R.string.tv_remote_navigation));
		mNavigationToggleBtn.setBackgroundResource(show ? R.drawable.btn_navigation_hide : R.drawable.btn_navigation_show);
		initControlPad();
	}
	
	private void initControlPad(){
		int layoutRes = ( !isChVolShown() && !isNavigationShown() ) 
				? R.layout.view_control_large : R.layout.view_control_small;
		mControlView.removeAllViews();
		mControlView = (FrameLayout) LayoutInflater.from(getContext()).inflate(layoutRes, mControlView, true);
		
		View v = mControlView.findViewById(R.id.abcd);
		if(v != null){
			v.setVisibility(isNavigationShown() ? View.GONE : View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		int code = -1;
		switch (v.getId()) {
			case R.id.a:
				code = KeyEvent.KEYCODE_A;
				break;
			case R.id.b:
				code = KeyEvent.KEYCODE_B;
				break;
			case R.id.c:
				code = KeyEvent.KEYCODE_C;
				break;
			case R.id.d:
				code = KeyEvent.KEYCODE_D;
				break;
	
			
			default:
				break;
		}
		RemoteKeyEvent keyDownEvent = new RemoteKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, code));
		mController.println(keyDownEvent.toString());
		RemoteKeyEvent keyUpEvent = new RemoteKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, code));
		mController.println(keyUpEvent.toString());
	}
}
